home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / chpass / RCS / util.c,v < prev   
Encoding:
Text File  |  1990-07-12  |  4.3 KB  |  184 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.07.11.18.15.35;  author shirriff;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  * Copyright (c) 1988 The Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms are permitted
  30.  * provided that the above copyright notice and this paragraph are
  31.  * duplicated in all such forms and that any documentation,
  32.  * advertising materials, and other materials related to such
  33.  * distribution and use acknowledge that the software was developed
  34.  * by the University of California, Berkeley.  The name of the
  35.  * University may not be used to endorse or promote products derived
  36.  * from this software without specific prior written permission.
  37.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  38.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  39.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  40.  */
  41.  
  42. #ifndef lint
  43. static char sccsid[] = "@@(#)util.c    5.9 (Berkeley) 3/27/89";
  44. #endif /* not lint */
  45.  
  46. #include <sys/types.h>
  47. #include <sys/time.h>
  48. #include <pwd.h>
  49. #include <stdio.h>
  50. #include <chpass.h>
  51. #include <strings.h>
  52. #include <ctype.h>
  53. #include "pathnames.h"
  54.  
  55. static int dmsize[] =
  56.     { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  57. static char *months[] =
  58.     { "January", "February", "March", "April", "May", "June",
  59.       "July", "August", "September", "October", "November",
  60.       "December", NULL };
  61. char *
  62. ttoa(tval)
  63.     time_t tval;
  64. {
  65.     struct tm *tp;
  66.     static char tbuf[50];
  67.  
  68.     if (tval) {
  69.         tp = localtime(&tval);
  70.         (void)sprintf(tbuf, "%s %d, 19%d", months[tp->tm_mon],
  71.             tp->tm_mday, tp->tm_year);
  72.     }
  73.     else
  74.         *tbuf = '\0';
  75.     return(tbuf);
  76.  
  77. atot(p, store)
  78.     char *p;
  79.     time_t *store;
  80. {
  81.     register char *t, **mp;
  82.     static struct tm *lt;
  83.     time_t tval, time();
  84.     int day, month, year;
  85.  
  86.     if (!*p) {
  87.         *store = 0;
  88.         return(0);
  89.     }
  90.     if (!lt) {
  91.         unsetenv("TZ");
  92.         (void)time(&tval);
  93.         lt = localtime(&tval);
  94.     }
  95.     if (!(t = strtok(p, " \t")))
  96.         goto bad;
  97.     for (mp = months;; ++mp) {
  98.         if (!*mp)
  99.             goto bad;
  100.         if (!strncasecmp(*mp, t, 3)) {
  101.             month = mp - months + 1;
  102.             break;
  103.         }
  104.     }
  105.     if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
  106.         goto bad;
  107.     day = atoi(t);
  108.     if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
  109.         goto bad;
  110.     year = atoi(t);
  111.     if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
  112.         goto bad;
  113.  
  114. #define    TM_YEAR_BASE    1900
  115. #define    EPOCH_YEAR    1970
  116. #define    DAYSPERNYEAR    365
  117. #define    DAYSPERLYEAR    366
  118. #define    HOURSPERDAY    24
  119. #define    MINSPERHOUR    60
  120. #define    SECSPERMIN    60
  121. #define    isleap(y) (((y) % 4) == 0 && ((y) % 100) != 0 || ((y) % 400) == 0)
  122.  
  123.     if (year < 100)
  124.         year += TM_YEAR_BASE;
  125.     if (year <= EPOCH_YEAR)
  126. bad:        return(1);
  127.     tval = isleap(year) && month > 2;
  128.     for (--year; year >= EPOCH_YEAR; --year)
  129.         tval += isleap(year) ?
  130.             DAYSPERLYEAR : DAYSPERNYEAR;
  131.     while (--month)
  132.         tval += dmsize[month];
  133.     tval += day;
  134.     tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN;
  135.     tval -= lt->tm_gmtoff;
  136.     *store = tval;
  137.     return(0);
  138. }
  139.  
  140. print(fp, pw)
  141.     FILE *fp;
  142.     struct passwd *pw;
  143. {
  144.     register char *p;
  145.     char *getusershell(), *ttoa();
  146.  
  147.     fprintf(fp, "#Changing user database information for %s.\n",
  148.         pw->pw_name);
  149.     if (!uid) {
  150.         fprintf(fp, "Login: %s\n", pw->pw_name);
  151.         fprintf(fp, "Password: %s\n", pw->pw_passwd);
  152.         fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
  153.         fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
  154.         fprintf(fp, "Change [month day year]: %s\n", ttoa(pw->pw_change));
  155.         fprintf(fp, "Expire [month day year]: %s\n", ttoa(pw->pw_expire));
  156.         fprintf(fp, "Class: %s\n", pw->pw_class);
  157.         fprintf(fp, "Home directory: %s\n", pw->pw_dir);
  158.         fprintf(fp, "Shell: %s\n",
  159.             *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
  160.     }
  161.     else {
  162.         /* only admin can change "restricted" shells */
  163.         setusershell();
  164.         for (;;)
  165.             if (!(p = getusershell()))
  166.                 break;
  167.             else if (!strcmp(pw->pw_shell, p)) {
  168.                 fprintf(fp, "Shell: %s\n", *pw->pw_shell ?
  169.                     pw->pw_shell : _PATH_BSHELL);
  170.                 break;
  171.             }
  172.     }
  173.     p = strsep(pw->pw_gecos, ",");
  174.     fprintf(fp, "Full Name: %s\n", p ? p : "");
  175.     p = strsep((char *)NULL, ",");
  176.     fprintf(fp, "Location: %s\n", p ? p : "");
  177.     p = strsep((char *)NULL, ",");
  178.     fprintf(fp, "Office Phone: %s\n", p ? p : "");
  179.     p = strsep((char *)NULL, ",");
  180.     fprintf(fp, "Home Phone: %s\n", p ? p : "");
  181. }
  182. @
  183.